﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.ViewModel.Config;
using Kovan.Model.Parameter.Config;
using Kovan.Model.Entity.Config;

namespace Kovan.DAL.Config
{
    public class DNewsManage
    {
        public IEnumerable<ENews> GetNewsItems(PNews param)
        {
            using (var db = new MysqlContext())
            {
                var query = from n in db.tb_news
                            join nd in db.tb_newsData on n.news_id.Value equals nd.news_id.Value into left
                            from cleft in left.DefaultIfEmpty()
                            orderby n.news_id.Value descending
                            select new ENews
                            {
                                NewsBase = n,
                                NewsData = cleft
                            };

                if (param.moduleId.HasValue)
                {
                    query = query.Where(o => o.NewsBase.module_id.Value == param.moduleId.Value);
                }

                if (param.isIndex.HasValue)
                {
                    query = query.Where(o => o.NewsBase.is_index.Value == param.isIndex.Value);
                }

                if (param.isView.HasValue)
                {
                    query = query.Where(o => o.NewsBase.is_view.Value == param.isView.Value);
                }

                if (param.isTop.HasValue)
                {
                    query = query.Where(o => o.NewsBase.is_top.Value == param.isTop.Value);
                }

                if (!string.IsNullOrEmpty(param.title))
                {
                    query = query.Where(o => o.NewsBase.title.Contains(param.title));
                }

                //总条数
                param.TotalRows = query.Count();

                if (param.PageSize > 0 && param.PageIndex > 0)
                {
                    query = query.Skip(param.PageSize * (param.PageIndex - 1)).Take(param.PageSize);
                }

                return query.OrderByDescending(c => c.NewsBase.news_id.Value).ToList();
            }
        }

        public ENews GetNewsItem(long id)
        {
            using (var db = new MysqlContext())
            {
                var query = from n in db.tb_news
                            where n.news_id.Value == id
                            join nd in db.tb_newsData on n.news_id.Value equals nd.news_id.Value into left
                            from cleft in left.DefaultIfEmpty()
                            orderby n.news_id.Value descending
                            select new ENews
                            {
                                NewsBase = n,
                                NewsData = cleft
                            };
                return query.FirstOrDefault();
            }
        }

        ///// <summary>
        ///// 添加
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns></returns>
        //public int Add(ENews model)
        //{
        //    using (var db = new MysqlContext())
        //    {
        //        var modelBase = model.NewsBase;
        //        var modelData = model.NewsData;

        //        db.tb_news.Add(modelBase);
        //        db.tb_newsData.Add(modelData);


        //        return db.SaveChanges();
        //    }
        //}

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(ENews model)
        {
            using (var db = new MysqlContext())
            {
                var modelBase = model.NewsBase;
                var modelData = model.NewsData;

                db.Set<tb_news>().Attach(modelBase);
                db.Entry(modelBase).State = System.Data.EntityState.Modified;

                db.Set<tb_newsData>().Attach(modelData);
                db.Entry(modelData).State = System.Data.EntityState.Modified;

                return db.SaveChanges();
            }
        }
    }
}
